package com.zx.tv;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.zx.base.http.HttpUtils;
import com.zx.base.http.URLHelper;
import com.zx.tv.app.NewTVApp;
import com.zx.tv.ui.WeatherActivity;

public class StartActivity extends Activity {

	private static final String MUSICID = "musicid";

	private SharedPreferences sp;

	private String mLocalId = "1";
	private String mServerId = "0";

	private NewTVApp app;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:

				new getMusic().execute(URLHelper.mPrefix);

				break;
			case 1:

				String result = (String) msg.obj;
				parseJson(result);

				break;
			case 2:

				Intent intent = new Intent(StartActivity.this,
						WeatherActivity.class);
				startActivity(intent);
				StartActivity.this.finish();

				break;
			case 3:
				// 去下载
				down();
				break;

			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startactivity);

		Intent intent = new Intent(this, MusicReceiver.class);

		Bundle bundle = new Bundle();
		bundle.putInt("op", 1);
		intent.putExtras(bundle);

		sendBroadcast(intent);

		app = (NewTVApp) getApplication();

		sp = getSharedPreferences(MUSICID, Activity.MODE_PRIVATE);

		mLocalId = sp.getString("id", "1");

		Message msg = new Message();
		msg.what = 0;
		handler.sendMessage(msg);
	}

	private void down() {

		Log.e("TAG", "in down");

		Intent intent = new Intent(this, DownReceiver.class);
		Bundle bundle = new Bundle();
		bundle.putInt("op", 7);
		intent.putExtras(bundle);
		sendBroadcast(intent);

		Message msg = new Message();
		msg.what = 2;
		handler.sendMessage(msg);
	}

	class getMusic extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "music");
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			Log.e("TAG", "music result = " + result);

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Message msg = new Message();
				msg.what = 2;
				handler.sendMessage(msg);
			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Message msg = new Message();
				msg.what = 2;
				handler.sendMessage(msg);
			} else {
				// 发送解析数据 消息
				Message msg = new Message();
				msg.obj = result;
				msg.what = 1;
				handler.sendMessage(msg);
			}

		}
	}

	private void parseJson(String str) {
		try {
			String succeed = new JSONObject(str).getString("succeed");
			if (succeed.equals("000")) {
				JSONObject json = new JSONObject(str);
				app.music.setId(json.getString("id"));
				app.music.setName(json.getString("name"));
				JSONObject weather = json.getJSONObject("dataList");
				app.music.setUrl(json.getString("url"));
				app.weather.setCity(weather.getString("city"));
				app.weather.setDate(weather.getString("date"));
				app.weather.setImg(weather.getString("img"));
				app.weather.setTemp(weather.getString("temp"));
				app.weather.setWeathers(weather.getString("weathers"));

				mServerId = json.getString("id");

				Log.e("TAG", "localid = " + mLocalId);
				Log.e("TAG", "serverId = " + mServerId);

				if (mServerId.equals(mLocalId)) {
					Message msg = new Message();
					msg.what = 2;
					handler.sendMessage(msg);
				} else {
					Message msg = new Message();
					msg.what = 3;
					handler.sendMessage(msg);
				}

			} else {
				Message msg = new Message();
				msg.what = 2;
				handler.sendMessage(msg);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

	}

}
